//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Calendar.h"
using namespace Cephei::QL::Times;
#include <gen/QL/Times/Period.h>
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Times::CCalendar::CCalendar () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phCalendar = NULL;
#endif
        _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (new QuantLib::Calendar ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::CCalendar::CCalendar (boost::shared_ptr<QuantLib::Calendar>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCalendar = NULL;
#endif
	_ppCalendar = &childNative;
    
}
Cephei::QL::Times::CCalendar::CCalendar (QuantLib::Calendar& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCalendar = NULL;
#endif
	_ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (&childNative);
    
    _CalendarOwner = owner;
    
}

Cephei::QL::Times::CCalendar::CCalendar (CCalendar^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCalendar = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (copy->GetShared());
        
    }
}
Cephei::QL::Times::CCalendar::CCalendar (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCalendar = NULL;
#endif
	if (!t->IsSubclassOf(CCalendar::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::CCalendar::CCalendar (QuantLib::Handle<QuantLib::Calendar>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCalendar = &childNative;
	_ppCalendar = &static_cast<boost::shared_ptr<QuantLib::Calendar>>(childNative.currentLink());
    
    _CalendarOwner = owner;
}
Cephei::QL::Times::CCalendar::CCalendar (QuantLib::Handle<QuantLib::Calendar> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCalendar = &childNative;
	_ppCalendar = &static_cast<boost::shared_ptr<QuantLib::Calendar>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Times::CCalendar::CCalendar (QuantLib::Calendar childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCalendar = NULL;
#endif
	_ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (new QuantLib::Calendar (childNative));
    
}
#endif

Cephei::QL::Times::CCalendar::~CCalendar ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCalendar != NULL)
    {
	    delete _ppCalendar;
        _ppCalendar = NULL;
    }
}
Cephei::QL::Times::CCalendar::!CCalendar ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCalendar != NULL)
    {
	    delete _ppCalendar;
    }
}
QuantLib::Calendar& Cephei::QL::Times::CCalendar::GetReference ()
{
    if (_ppCalendar == NULL) throw REFNEW NativeNullException ();
	return **_ppCalendar;
}
boost::shared_ptr<QuantLib::Calendar>& Cephei::QL::Times::CCalendar::GetShared ()
{
    if (_ppCalendar == NULL) throw REFNEW NativeNullException ();
	return *_ppCalendar;
}
QuantLib::Calendar* Cephei::QL::Times::CCalendar::GetPointer ()
{
    if (_ppCalendar == NULL) throw REFNEW NativeNullException ();
	return &**_ppCalendar;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Calendar>& Cephei::QL::Times::CCalendar::GetHandle ()
{
	if (_phCalendar == NULL)
	{
		_phCalendar = new Handle<QuantLib::Calendar> (*_ppCalendar);
	}
	return *_phCalendar;
}
#endif
bool Cephei::QL::Times::CCalendar::HasNative () 
{
	return (_ppCalendar != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::Times::CCalendar::AddHoliday (DateTime d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	(*_ppCalendar)->addHoliday ( _d );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CCalendar::Adjust (DateTime d, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ convention)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
        QuantLib::BusinessDayConvention _convention = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>::IsSome::get (convention) ? (QuantLib::BusinessDayConvention)convention->Value : QuantLib::Following ); //5
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCalendar)->adjust ( _d,  _convention );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CCalendar::Advance (DateTime date, Cephei::QL::Times::IPeriod^ period, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ convention, Microsoft::FSharp::Core::FSharpOption<Boolean>^ endOfMonth)
{
    CPeriod^ _Cperiod;
    try
    {
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date); //a
        _Cperiod = safe_cast<CPeriod^> (period);
        _Cperiod->Lock();
        QuantLib::Period& _period = static_cast<QuantLib::Period&> (_Cperiod->GetReference ()); 
        QuantLib::BusinessDayConvention _convention = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>::IsSome::get (convention) ? (QuantLib::BusinessDayConvention)convention->Value : QuantLib::Following ); //5
        bool _endOfMonth = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (endOfMonth) ? (bool)ValueHelper::Convert (endOfMonth->Value) : false); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCalendar)->advance ( _date,  _period,  _convention,  _endOfMonth );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cperiod != nullptr) _Cperiod->Unlock();
    }
}
DateTime Cephei::QL::Times::CCalendar::Advance (DateTime d, Int32 n, QL::Times::TimeUnitEnum unit, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ convention, Microsoft::FSharp::Core::FSharpOption<Boolean>^ endOfMonth)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
        QuantLib::Integer _n = (QuantLib::Integer)ValueHelper::Convert (n); //a
        QuantLib::TimeUnit _unit = (QuantLib::TimeUnit)unit ;
        QuantLib::BusinessDayConvention _convention = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>::IsSome::get (convention) ? (QuantLib::BusinessDayConvention)convention->Value : QuantLib::Following ); //5
        bool _endOfMonth = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (endOfMonth) ? (bool)ValueHelper::Convert (endOfMonth->Value) : false); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCalendar)->advance ( _d,  _n,  _unit,  _convention,  _endOfMonth );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Int64 Cephei::QL::Times::CCalendar::BusinessDaysBetween (DateTime from, DateTime to, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeFirst, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeLast)
{
    try
    {
        QuantLib::Date _from = (QuantLib::Date)ValueHelper::Convert (from); //a
        QuantLib::Date _to = (QuantLib::Date)ValueHelper::Convert (to); //a
        bool _includeFirst = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (includeFirst) ? (bool)ValueHelper::Convert (includeFirst->Value) : true); //9a
        bool _includeLast = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (includeLast) ? (bool)ValueHelper::Convert (includeLast->Value) : false); //9a
    	QuantLib::BigInteger _rv = (QuantLib::BigInteger)(*_ppCalendar)->businessDaysBetween ( _from,  _to,  _includeFirst,  _includeLast );   
        Int64 _nrv = (Int64)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Times::CCalendar::Empty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCalendar)->empty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CCalendar::EndOfMonth (DateTime d)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCalendar)->endOfMonth ( _d );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<DateTime>^ Cephei::QL::Times::CCalendar::HolidayList (Cephei::QL::Times::ICalendar^ calendar, DateTime from, DateTime to, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeWeekEnds)
{
    CCalendar^ _Ccalendar;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Date _from = (QuantLib::Date)ValueHelper::Convert (from); //a
        QuantLib::Date _to = (QuantLib::Date)ValueHelper::Convert (to); //a
        bool _includeWeekEnds = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (includeWeekEnds) ? (bool)ValueHelper::Convert (includeWeekEnds->Value) : false); //9a
    	std::vector<QuantLib::Date> _rv = (std::vector<QuantLib::Date>)(*_ppCalendar)->holidayList ( _calendar,  _from,  _to,  _includeWeekEnds );   
        Cephei::Core::IVector<DateTime>^ _nrv = REFNEW CoVector<DateTime> (REFNEW CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
    }
}
Boolean Cephei::QL::Times::CCalendar::IsBusinessDay (DateTime d)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	bool _rv = (bool)(*_ppCalendar)->isBusinessDay ( _d );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Times::CCalendar::IsEndOfMonth (DateTime d)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	bool _rv = (bool)(*_ppCalendar)->isEndOfMonth ( _d );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Times::CCalendar::IsHoliday (DateTime d)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	bool _rv = (bool)(*_ppCalendar)->isHoliday ( _d );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Times::CCalendar::IsWeekend (QL::Times::WeekdayEnum w)
{
    try
    {
        QuantLib::Weekday _w = (QuantLib::Weekday)w ;
    	bool _rv = (bool)(*_ppCalendar)->isWeekend ( _w );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Times::CCalendar::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppCalendar)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::ICalendar^ Cephei::QL::Times::CCalendar::RemoveHoliday (DateTime d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	(*_ppCalendar)->removeHoliday ( _d );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Times::ICalendar^ Cephei::QL::Times::CCalendar_Factory::Create ()
{
    return REFNEW CCalendar ();
}
